define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'booking/house/index' + location.search,
                    add_url: 'booking/house/add',
                    edit_url: 'booking/house/edit',
                    del_url: 'booking/house/del',
                    multi_url: 'booking/house/multi',
                    import_url: 'booking/house/import',
                    table: 'booking_house',
                }
            });

            var table = $("#table");

            var columns = [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: __('Id')
                },
                {
                    field: 'store_id',
                    title: __('Store_id'),
                    formatter: Table.api.formatter.search
                },
                {
                    field: 'store.name',
                    title: __('Store'),
                    formatter: Controller.api.content,
                    operate: false
                },
                {
                    field: 'name',
                    title: __('Name'),
                    formatter: Controller.api.content,
                    operate: 'LIKE'
                },
                {
                    field: 'floor_config_id',
                    title: __('Floor_config_id'),
                    visible: false
                },
                {
                    field: 'floor_config.name',
                    title: __('Floor_config'),
                    operate: false
                },
                {
                    field: 'room',
                    title: __('Room'),
                    operate: 'LIKE'
                },
                {
                    field: 'type_config_id',
                    title: __('Type_config_id'),
                    visible: false

                },
                {
                    field: 'type_config.name',
                    title: __('Type_config'),
                    operate: false
                },
                {
                    field: 'tag_config_ids',
                    title: __('Tag_config'),
                    operate: false,
                    visible: false,
                    formatter: function (val, row) {
                        if (row.group_name && row.group_name.tag) {
                            return row.group_name.tag.join(',');
                        }
                    }
                },
                {
                    field: 'facility_config_ids',
                    title: __('Facility_config'),
                    operate: false,
                    formatter: function (val, row) {
                        if (row.group_name && row.group_name.facility) {
                            return row.group_name.facility.join(',');
                        }
                    }
                },
                {
                    field: 'market_price',
                    title: __('Market_price'),
                    operate: 'BETWEEN'
                },
                {
                    field: 'price',
                    title: __('Price'),
                    operate: 'BETWEEN'
                },

                {
                    field: 'views',
                    title: __('Views')
                },
                {
                    field: 'images',
                    title: __('Images'),
                    operate: false,
                    events: Table.api.events.image,
                    formatter: Table.api.formatter.images
                },
                {
                    field: 'area',
                    title: __('Area'),
                    operate: 'BETWEEN'
                },
                {
                    field: 'livenums',
                    title: __('Livenums')
                },
                {field: 'weigh', title: __('Weigh'), operate: false},
                {
                    field: 'status',
                    title: __('Status'),
                    searchList: {
                        "up": __('Status up'),
                        "down": __('Status down'),
                        "audit":__('Status audit')
                        
                    },
                    formatter: Table.api.formatter.status
                }
            ];
            if (typeof Config.fields !== 'undefined') {
                //动态追加字段
                $.each(Config.fields, function (i, j) {
                    var data = {
                        field: "house_form." + j.name,
                        title: j.title,
                        operate: false,
                        cellStyle: {
                            classes: "applydata"
                        }
                    };
                    //如果是图片,加上formatter
                    if (j.type == 'image') {
                        data.events = Table.api.events.image;
                        data.formatter = Table.api.formatter.image;
                    } else if (j.type == 'images') {
                        data.events = Table.api.events.image;
                        data.formatter = Table.api.formatter.images;
                    } else if (j.type == 'radio' || j.type == 'checkbox' || j.type == 'select' || j.type == 'selects') {
                        data.formatter = Controller.api.formatter.content;
                        data.extend = j.content_list;
                        data.searchList = j.content_list;
                    }
                    columns.push(data);
                });
            }

            columns = columns.concat([
                {
                    field: 'createtime',
                    title: __('Createtime'),
                    operate: 'RANGE',
                    addclass: 'datetimerange',
                    autocomplete: false,
                    formatter: Table.api.formatter.datetime
                },
                {
                    field: 'updatetime',
                    title: __('Updatetime'),
                    operate: 'RANGE',
                    addclass: 'datetimerange',
                    autocomplete: false,
                    formatter: Table.api.formatter.datetime
                },
                {
                    field: 'operate',
                    title: __('Operate'),
                    table: table,
                    events: Table.api.events.operate,
                    formatter: Table.api.formatter.operate,
                    buttons: [{
                        name: '日历价格',
                        title: __('日历价格'),
                        classname: 'btn btn-xs btn-primary btn-dialog',
                        text: '日历价格',
                        icon: 'fa fa-calendar',
                        extend: 'data-area=\'["80%","80%"]\'',
                        url: 'booking/calendar/index/house_id/{id}',
                        callback: function (data) {
                        }
                    }]
                }
            ]);

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [columns]
            });

            $('.btn-diy-field').click(function () {
                Fast.api.open('booking/fields/index/source/house', '自定义字段', {
                    area: ["80%", "80%"]
                })
            });

            $('.btn-house-search').click(function () {
                Fast.api.open('booking/house/search', '房间预订状态', {
                    area: ["80%", "80%"]
                })
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        search: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'booking/house/search' + location.search,
                    table: 'booking_house',
                }
            });
            Template.helper("Moment", Moment);
            var table = $("#table");
            var columns = [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: __('Id')
                },
                {
                    field: 'store_id',
                    title: __('Store_id'),
                    visible: false,
                    formatter: Table.api.formatter.search
                },
                {
                    field: 'store.name',
                    title: __('Store'),
                    operate: false
                },
                {
                    field: 'name',
                    title: __('Name'),
                    operate: 'LIKE',
                    formatter: Controller.api.content
                },
                {
                    field: 'images',
                    title: __('Images'),
                    operate: false,
                    events: Table.api.events.image,
                    formatter: Table.api.formatter.images
                },
                {
                    field: 'room',
                    title: __('Room'),
                    operate: 'LIKE'
                },
                {
                    field: 'market_price',
                    title: __('Market_price'),
                    operate: 'BETWEEN'
                },
                {
                    field: 'price',
                    title: __('Price'),
                    operate: 'BETWEEN'
                },

                {
                    field: 'area',
                    title: __('Area'),
                    operate: 'BETWEEN'
                },
                {
                    field: 'livenums',
                    title: __('Livenums')
                }
            ];

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                templateView: true,
                pk: 'id',
                sortName: 'id',
                columns: [columns]
            });

            var ranges = [];
            ranges[__('Today')] = [Moment().startOf('day'), Moment().subtract(-1, 'days').startOf('day')];
            ranges[__('Tomorrow')] = [Moment().subtract(-1, 'days').startOf('day'), Moment().subtract(-2, 'days').endOf('day')];
            ranges[__('The next 3 days')] = [Moment().startOf('day'), Moment().subtract(-3, 'days').endOf('day')];
            ranges[__('The next 7 days')] = [Moment().startOf('day'), Moment().subtract(-7, 'days').endOf('day')];

            var calendar = store_id = '';
            calendar = $("#c-calendar").val();

            var render = function () {
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    params.calendar = calendar;
                    params.store_id = store_id;
                    return params;
                };
                table.bootstrapTable('refresh', {});
            };

            $("#c-calendar").data("callback", function (start, end, label) {
                if (typeof label != 'undefined') {
                    $(this.element).val(start.format("YYYY-MM-DD") + " - " + end.format("YYYY-MM-DD"));
                    $(this.element).trigger('blur');
                    calendar = start.format("YYYY-MM-DD") + " - " + end.format("YYYY-MM-DD");
                    render();
                }
            }).data("ranges", ranges);

            $('#c-store_id').on('change', function () {
                store_id = $('#c-store_id').val();
                render();
            });

            $('#table').delegate('.add-order', 'click', function () {
                let house_id = $(this).data('house_id');
                Fast.api.open('booking/order/add/house_id/' + house_id + '/calendar/' + calendar, '房间预订', {
                    callback: function () {

                    }
                })
            });

            Form.events.daterangepicker($('.toolbar'));
            Form.events.selectpage($('.toolbar'));
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        addOrEdit() {
            $('#c-to_shop_pay').on('change', function () {
                if ($(this).val() == 1) {
                    $('.interval_time').removeClass('hide');
                } else {
                    $('.interval_time').addClass('hide');
                }
            })
            $(document).on("fa.event.appendfieldlist", "form[role=form] .btn-append", function (e, obj) {
                Form.events.selectpicker(obj);
            });
        },
        add: function () {
            this.addOrEdit();
            Controller.api.bindevent();
        },
        edit: function () {
            this.addOrEdit();
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            content: function (value, row, index) {
                var width = this.width != undefined ? (this.width.match(/^\d+$/) ? this.width + "px" : this.width) : "250px";
                return "<div style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + ";' title='" + value + "' data-toggle='tooltip' data-placement='right'>" + value + "</div>";
            },
        }
    };
    return Controller;
});
